#ifndef DEVICEMANAGER_H
#define DEVICEMANAGER_H

#include <QObject>
#include <QMap>
#include <QJsonObject>
#include <QString>
#include "device.h"

class DeviceManager : public QObject
{
    Q_OBJECT

public:
    explicit DeviceManager(QObject *parent = nullptr);
    ~DeviceManager();

    // 设备管理方法
    void addDevice(const QString &ipAddress, const QString &deviceId);
    void removeDevice(const QString &ipAddress);
    Device* getDevice(const QString &ipAddress) const;
    bool hasDevice(const QString &ipAddress) const;
    QList<Device*> getAllDevices() const;
    
    // 批量操作方法
    void connectToDevice(const QString &ipAddress);
    void disconnectFromDevice(const QString &ipAddress);
    bool isConnected(const QString &ipAddress) const;
    
    // 登录相关方法
    void requestChallenge(const QString &ipAddress, const QString &username);
    void login(const QString &ipAddress, const QString &username, const QString &challenge, 
               const QString &credential, bool rememberMe = false);
               
    // 网络配置相关方法
    void getNetworkConfig(const QString &ipAddress);
    void setNetworkConfig(const QString &ipAddress, const QString &interfaceName, bool enable, 
                         const QString &mode, const QString &ipAddressParam = QString(), 
                         const QString &netmask = QString(), const QString &gateway = QString());
    
    // 固件升级相关方法
    void uploadFirmware(const QString &ipAddress, int chunkIndex, int totalChunks, const QByteArray &chunkData, 
                       const QString &fileName = QString(), qint64 fileSize = 0, 
                       const QString &version = QString(), const QString &md5 = QString());
    void startFirmwareUpgrade(const QString &ipAddress, const QString &version, bool force = false);
    void getFirmwareUpgradeStatus(const QString &ipAddress);

signals:
    // 设备相关信号
    void deviceAdded(const QString &ipAddress);
    void deviceRemoved(const QString &ipAddress);
    
    // 连接状态变化信号
    void connectionStateChanged(const QString &ipAddress, bool connected);
    // 错误信号
    void errorOccurred(const QString &ipAddress, const QString &errorMessage);
    // 登录相关信号
    void challengeReceived(const QString &ipAddress, const QString &challenge, int expiresIn);
    void loginSuccess(const QString &ipAddress, const QString &accessToken, const QString &refreshToken, 
                     const QJsonObject &userInfo);
    void loginFailed(const QString &ipAddress, int errorCode, const QString &errorMessage);
    // 网络配置相关信号
    void networkConfigReceived(const QString &ipAddress, const QJsonObject &config);
    void networkConfigSetResult(const QString &ipAddress, bool success, const QString &message);
    
    // 固件升级相关信号
    void firmwareUploadProgress(const QString &ipAddress, int progress);
    void firmwareUploadComplete(const QString &ipAddress);
    void firmwareUpgradeStarted(const QString &ipAddress, const QString &upgradeId, const QString &status, int estimatedDuration);
    void firmwareUpgradeStatusReceived(const QString &ipAddress, const QString &status, int progress, const QString &message);
    void firmwareUpgradeError(const QString &ipAddress, const QString &errorMessage);

private slots:
    // 设备信号转发槽函数
    void onDeviceConnectionStateChanged(const QString &ipAddress, bool connected);
    void onDeviceError(const QString &ipAddress, const QString &errorMessage);
    void onDeviceChallengeReceived(const QString &ipAddress, const QString &challenge, int expiresIn);
    void onDeviceLoginSuccess(const QString &ipAddress, const QString &accessToken, const QString &refreshToken, 
                             const QJsonObject &userInfo);
    void onDeviceLoginFailed(const QString &ipAddress, int errorCode, const QString &errorMessage);
    void onDeviceNetworkConfigReceived(const QString &ipAddress, const QJsonObject &config);
    void onDeviceNetworkConfigSetResult(const QString &ipAddress, bool success, const QString &message);
    
    // 固件升级相关槽函数
    void onDeviceFirmwareUploadProgress(const QString &ipAddress, int progress);
    void onDeviceFirmwareUploadComplete(const QString &ipAddress);
    void onDeviceFirmwareUpgradeStarted(const QString &ipAddress, const QString &upgradeId, const QString &status, int estimatedDuration);
    void onDeviceFirmwareUpgradeStatusReceived(const QString &ipAddress, const QString &status, int progress, const QString &message);
    void onDeviceFirmwareUpgradeError(const QString &ipAddress, const QString &errorMessage);

private:
    QMap<QString, Device*> m_devices;  // IP地址到设备对象的映射
    QString m_deviceId;                // 客户端设备ID
};

#endif // DEVICEMANAGER_H